<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('课程修改')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-course-edit" th:object="${course}">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">课程编号：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="id" id="id" th:field="*{id}" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">课程名：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="course1" id="course1" th:field="*{course1}" required>
            </div>
        </div>
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label is-required">课程照片：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <input class="form-control" type="text" name="coursepicture" id="coursepicture" th:field="*{coursepicture}" required>-->
<!--            </div>-->
<!--        </div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">课程照片：</label>
            <div class="col-sm-8">
                <p class="user-info-head" onclick="courseAvatar()">
                    <img class="img-circle img-lg"
                         th:src="${#strings.isEmpty(course.coursepicture)} ? @{/img/profile.jpg} : @{${course.coursepicture}}"
                         onerror="this.src='/img/profile.jpg'"
                         alt="课程照片">
                </p>
                <p><a href="javascript:void(0);" onclick="courseAvatar()">修改课程照片</a></p>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-8">
                <button type="button" class="btn btn-primary" onclick="submitHandler()">保存</button>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    var prefix = ctx + "system/course";

    $("#form-course-edit").validate({
        onkeyup: false,
        rules: {
            id: {
                required: true,
                number: true,
                range: [0, 100]
            },
            course1: {
                required: true
            }
        },
        messages: {
            id: {
                required: "课程编号不能为空",
                number: "课程编号必须是数字",
                range: "课程编号必须在0到100之间"
            },
            course1: {
                required: "课程名不能为空"
            }
        }
    });

    function submitHandler() {
        if ($("#form-course-edit").valid()) {
            $.operate.save(prefix + "/edit", $('#form-course-edit').serialize());
        }
    }
    function courseAvatar() {
        //var courseId = document.getElementById('id').value;
        var courseId = document.querySelector('input[name="id"]').value;
        if (!courseId) {
            alert("课程编号为空，请刷新页面或联系管理员！");
            return;
        }
        var url = ctx + 'system/course/avatar/' + courseId;
        top.layer.open({
            type: 2,
            area: [$(window).width() + 'px', $(window).height() + 'px'],
            fix: false,
            maxmin: true,
            shade: 0.3,
            title: "修改课程照片",
            content: url,
            btn: ['确定', '关闭'],
            shadeClose: true,
            // 点击确定按钮
            yes: function(index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandlePicture(index, layero);
            },
            cancel: function(index) {
                return true;
            }
        });
    }
    // function submitHandlePicture() {
    //     if ($.validate.form()) {
    //         $.operate.save(prefix + "/avatar", $('#form-course-avatar').serialize());
    //     }
    // }

</script>
</body>
</html>
